/* TextTree.h
Michael Zahniser, 2008

Header file for the TextTree class, which represents a tree in text form, with
nodes that can be expanded or collapsed.
*/

#ifndef __MZ_TEXT_TREE_H__
#define __MZ_TEXT_TREE_H__

#include <string>
#include <list>

using std::string;
using std::list;



class TextTree {
public:
	class Iterator;
	
	TextTree();

	void Clear();
	void Add(int indent, const string &text);


private:
	class Line {
	public:
		Line(int indent, const string &text) 
			: text(string(4 * indent, ' ') + "[ ] " + text)
		{ 
			mode = this->text.begin() + 4 * indent + 1;
		}
		Line(const Line &line) { *this = line; }
		
		Line &operator=(const Line &line)
		{
			text = line.text;
			mode = text.begin() + (line.mode - line.text.begin());
			return *this;
		}
		
		const string &ToString() const { return text; }
		char Mode() const { return *mode; }
		void SetMode(char newMode) { *mode = newMode; }
		int Indent() const { return static_cast<int>(mode - text.begin()) / 4; }
		
	private:
		string text;
		string::iterator mode;
	};
	
	
private:
	list<Line> lines;
	int previousIndent;
	
	friend class TextTree::Iterator;
	
	
public:
	class Iterator {
	public:
		Iterator() { it = end; }
		Iterator(TextTree &tree);
		
		void operator++();
		void operator++(int);
		
		operator bool() const;
		bool operator!() const;
		
		const string &operator*() const;
		const string *operator->() const;
		
		void Expand();
		void Collapse();
		
	private:
		void FindNext();
		
	private:
		list<Line>::iterator it;
		list<Line>::iterator end;
		
		friend class TextTree;
	};
};



#endif
